import modules from './modules'
import Vue from 'vue'
import Router from '@/plugin/uni-simple-router/index.js'
import {ACCESS_TOKEN} from '@/common/util/constants.js'

Vue.use(Router)
//初始化
const router = new Router({
	encodeURI:true,  
    routes: [...modules]//路由表
});

const whiteList = ['/pages/login/login','/pages/login/readLogin'] 
//全局路由前置守卫
router.beforeEach((to, from, next) => {	
	var url =  location.href;
	//alert("url:"+url);
	let token=uni.getStorageSync(ACCESS_TOKEN);
	//alert("token:"+token);

	if(token){
		next()
	}else{
		if (whiteList.indexOf(to.path) !== -1) {
			//从本地缓存中同步移除指定 key
			uni.removeStorageSync(ACCESS_TOKEN);
			next()
		}else{
		  if(url.indexOf('?') !== -1){
			  let param=url.substring(url.indexOf('?')+1);
			  next({path: '/pages/login/login', query: {param}})
		  }else{
			  next({path: '/pages/login/login'})
			  // next({ path: '/pages/login/login'})
		  }
		}
	} 
})
// 全局路由后置守卫
router.afterEach((to, from) => {
	console.log("afterEach")
})
export default router;